<?php
class ProductsList
{
	//Public variable to  be read from smarty template 
	public $mPage =1;
	public $mrTotalPages;
	public $mLinkToNextPage;
	public $mLinkToPreviousPage;
	public $mProducts;
	public $mProductListPages =array();
	public $mSearchDescription;
	public $mAllWords ='off';
	public $mSearchString;
	//public $mSearchDescription;
	
	//Private members 
	private $_mDepartmentId;
	private $_mCategoryId;
	
	//Class constructor 
	public function __construct(){
		
		//Get DepartmentId from query string casting it to int 
		if(isset($_GET['DepartmentId'])){
			$this->_mDepartmentId =(int)$_GET['DepartmentId'];
		}
		
		//Get category id from query string casting it to int 
		if(isset($_GET['CategoryId'])){
			$this->_mCategoryId =(int)$_GET['CategoryId'];
		}
		
		//Get page number from query string casting it to int 
		if(isset($_GET['Page']))
		$this->mPage =(int)$_GET['Page'];
		
		if($this->mPage <1)
		trigger_error('incorrect page value');
		
		//save page request for continue shopping functionality
		$_SESSION['link_to_continue_shopping']=$_SERVER['QUERY_STRING'];
		
	} 
	public function init(){
		
		if(isset($_GET['SearchResults']))
		{
			$this->mSearchString =trim(str_replace('-','',$_GET['SearchString']));
			$this->AllWords =isset($_GET['AllWords']) ? $_GET['AllWords'] :'off';
		}
		// if browsing a category , get the list of product from that category 
		if(isset($this->mSearchString))
		{
			//Get Search Results 
			$search_results =Catalog::Search($this->mSearchString, $this->mAllWords, $this->mPage, $this->mrTotalPages) ;
			//print_r($search_results['accepted_words']);
			print_r($search_results['ignored_words']);
			
			//Get the List of products 
			$this->mProducts =$search_results['products'];
			//Build the title for the list of products 
			if(count($search_results['accepted_words']) >0)
			$this->mSearchDescription ='<p class="description">Products Containing <font class ="words">'
			.($this->mAllWords == 'on' ? 'all' : 'any') .'</font>'
			.' of these words : <font class ="words" >'
			.implode(',',$search_results['accepted_words']) .
			'<font></p>';
			if(count($search_results['ignored_words']) > 0)
			$this->mSearchDescription .= 
			'<p class ="description" >Ignored words: <font class ="words">'
			.implode(',',$search_results['ignored_words']) .
			'</font></p>';
			
			if(!(count($search_results['products']) >0))
			$this->mSearchDescription .= '<p class ="description">Your Search Generated no results .</p>';
			
		}
		
		elseif(isset($this->_mCategoryId)){
			$this->mProducts =Catalog::GetProductsInCategory(
			$this->_mCategoryId, $this->mPage, $this->mrTotalPages);
		}
		//if  browsing the department , get the list of product from the department
		elseif(isset($this->_mDepartmentId)){
			$this->mProducts =Catalog::GetProductsInDepartment(
			$this->_mDepartmentId, $this->mPage, $this->mrTotalPages);
		}else {
			$this->mProducts =Catalog::GetProductsOnCatalog($this->mPage, $this->mrTotalPages);
		}
		
		// if subpages display  navigation controls 
		if($this->mrTotalPages >1){
			
			//Build next link 
			if($this->mPage < $this->mrTotalPages){
				if(isset($_GET['SearchResults']))
				$this->mLinkToNextPage =Link::ToSearchResults($this->mSearchString, $this->mAllWords,$this->mPage +1);
				elseif(isset($this->_mCategoryId)){
					$this->mLinkToNextPage =Link::ToCategory(
					$this->_mDepartmentId,$this->_mCategoryId,$this->mPage +1);
				}else if(isset($this->_mDepartmentId)){
					$this->mLinkToNextPage =Link::ToDepartment(
					$this->_mDepartmentId,$this->mPage +1);
				}else{
					$this->mLinkToNextPage =Link::ToIndex($this->mPage +1);
				}
			}
			
			//Bulid previous page 
			if($this->mPage > 1){
				if(isset($_GET['SearchResults']))
				$this->mLinkToPreviousPage =Link::ToSearchResults($this->mSearchString, $this->mAllWords,$this->mPage -1);
			    elseif(isset($this->_mCategoryId)){
					$this->mLinkToPreviousPage=Link::ToCategory(
					$this->_mDepartmentId,$this->_mCategoryId,$this->mPage -1);
				}else if($this->_mDepartmentId){
					$this->mLinkToPreviousPage =Link::ToDepartment(
					$this->_mDepartmentId,$this->mPage -1);
				}else{
					$this->mLinkToNextPage =Link::ToIndex($this->mPage - 1);
				}
			}
			
			for($i=1 ; $i<$this->mrTotalPages ;$i++){
				if(isset($_GET['SearchResults']))
				$this->mProductListPages[] =Link::ToSearchResults($this->mSearchString, $this->mAllWords,$i);
				elseif(isset($this->_mCategoryId))
				$this->mProductListPages[]=Link::ToCategory($this->_mDepartmentId,$this->_mCategoryId,$i);
				elseif(isset($this->_mDepartmentId))
				$this->mProductListPages[]=Link::ToDepartment($this->_mDepartmentId ,$i);
				else
					$this->mProductListPages[]=Link::ToIndex($i);
				
			}
		
			
		}
		
		
		    if($this->mPage > $this->mrTotalPages && !empty($this->mrTotalPages) )
		    {
			//Clean outout buffer 
			ob_clean();
			
			// load the 404 page 
			include '404.php';
			
			// clear the output buffer and stop execution 
			flush();
			ob_flush();
			ob_end_clean();
			exit();
			
		    }
		
		  // Build links for product detail page 
		  for($i=0;$i < count($this->mProducts);$i++){
			$this->mProducts[$i]['link_to_product'] =Link::ToProduct($this->mProducts[$i]['product_id']);
			if($this->mProducts[$i]['thumbnail']){
				$this->mProducts[$i]['thumbnail']=Link::Build('product_images/' .$this->mProducts[$i]['thumbnail']);
			}
			
			$this->mProducts[$i]['link_to_add_product'] =Link::ToAddProduct($this->mProducts[$i]['product_id']);
			
			$this->mProducts[$i]['attributes']=Catalog::GetProductAttributes($this->mProducts[$i]['product_id']);
			
		} 
		
		
		
		
		
		
	}
}